﻿
using Models.Common;
using Models.Device;
using UtilsBase;
using GatewayServerService.CloudCenter;

namespace GatewayServerService.Device
{
    /// <summary>
    ///普锐森社 土壤温湿度传感器水分变送器农业大棚土质肥力氮磷钾酸碱度检测仪
    /// </summary>
    public class ServiceDeviceRS4851016_TR
    {
        ServiceCloudDeviceValueLog log = new ServiceCloudDeviceValueLog();
        //回调
        public void callBackUpdate(MQReqModel<object> httpReqModel)
        {
            try
            {
                ModelReceverData modelDeviceData = httpReqModel.toServerData;
                CommandLogModel commandLogModel = httpReqModel.toGatewayData;

                if (string.IsNullOrWhiteSpace(modelDeviceData.commandResults[0].value))
                {
                    return;
                }
                if (commandLogModel.key == "collect")
                {
                    //普锐森社 土壤温湿度传感器水分变送器农业大棚土质肥力氮磷钾酸碱度检测仪
                    //https://detail.tmall.com/item.htm?app=chrome&bxsign=scdyIRcNhp2OlnAnDK2wwSsVyEhbXlIDj3SsPWtbpzYtQewW4l1_ret8XK7nVvu5kE7Mht8rAXi7k1nuzMZtv282S831EggqM6bCkF6DUNmyPtYYSnBwoXodSPKppjyNV5Q&cpp=1&id=593906558680&share_crt_v=1&shareurl=true&short_name=h.UJuuRon&sourceType=item&sp_abtk=gray_1_code_simpleAndroid2&sp_tk=bzFGdGRRZFJjWDk=&spm=a2159r.13376460.0.0&tbSocialPopKey=shareItem&tk=o1FtdQdRcX9%20CZ3457&ttid=1568860058617@taobao_android_10.22.10&un=87d1c8dd838bb7dde7c4cca5375f99dd&un_site=0&ut_sk=1.YSw0382wAckDAP1SKlIXnY+4_21646297_1679372679676.GoodsTitleURL.1
                    //温度计算：
                    //            当温度低于 0 ℃ 时温度数据以补码的形式上传。
                    //温度：FF9B H(十六进制)= -101 => 温度 = -10.1℃
                    //水分计算：
                    //水分：292 H(十六进制) = 658 => 湿度 = 65.8 %，即土壤体积含水率为 65.8 %。
                    //电导率计算：
                    //电导率：3E8 H(十六进制) = 1000 电导率 = 1000 us / cm
                    //PH 值计算：
                    //PH 值：38H（十六进制）= 56 => PH 值 = 5.6
                    //https://www.prsens.com/index.php?a=shows&catid=44&id=227

                    string result = modelDeviceData.commandResults[0].value;
                    string waterdataHex = result.Substring(9, 5);//水份
                    string temperdataHex = result.Substring(15, 5);//温度值
                    string ecdataHex = result.Substring(21, 5);//电导率值 
                    string phdataHex = result.Substring(27, 5);//PH 值

                    //水份
                    double water = new UtilsDataTypeChange().hex16Convert10(waterdataHex) * 0.1;
                    Console.WriteLine("water" + water + "%");
                    //电导率值 
                    double ec = new UtilsDataTypeChange().hex16Convert10(ecdataHex) * 1.0;
                    Console.WriteLine("ec" + ec + "μs/cm");
                    //PH 值
                    double ph = new UtilsDataTypeChange().hex16Convert10(phdataHex) * 0.1;
                    Console.WriteLine("ph" + ph);

                    //温度
                    double temperature = new UtilsDataTypeChange().hex16Convert10(temperdataHex) * 0.1;
                    Console.WriteLine("temperature" + temperature.ToString() + "℃");

                    ServiceCloudDeviceValueLog log = new ServiceCloudDeviceValueLog();
                    //  Dictionary<string, double> keyValuePairs = JsonConvert.DeserializeObject<Dictionary<string, double>>(modelDeviceData.content);
                    //写日志
                    log.writeSensorlog(commandLogModel.d_guid, "ec", UtilsNumber.GetNumberForLength(ec, 2).ToString(), "EC值");
                    log.writeSensorlog(commandLogModel.d_guid, "ph", UtilsNumber.GetNumberForLength(ph, 2).ToString(), "PH值");
                    log.writeSensorlog(commandLogModel.d_guid, "water", UtilsNumber.GetNumberForLength(water, 2).ToString(), "水份");
                    log.writeSensorlog(commandLogModel.d_guid, "temperature", UtilsNumber.GetNumberForLength(temperature, 2).ToString(), "温度");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }

    }

}
